import Taro, { Component } from '@tarojs/taro'
import { View } from '@tarojs/components'
import { connect } from '@tarojs/redux'

import TopicInfo from '../../components/topicInfo'
import { getTopicInfoAction, playTopicNiceAction } from '../../actions/topicListAction'

@connect((store) => {
  return {
    playStatus: store.topicListReducer.playStatus,
    user: store.userReducer,
    topicInfo: store.topicListReducer.topicInfo,
    replyTopicInfo: store.topicListReducer.replyTopicInfo
  }
}, (dispatch) => ({
  onGetTopicInfo(params){
    dispatch(getTopicInfoAction(params))
  },
  onPlayTopicNice(params){
    dispatch(playTopicNiceAction(params))
  }
}))
class Detail extends Component{
  config = {
    navigationBarTitleText: '话题详情'
  }
  componentDidMount(){
    this.getDetail()
    // let {user} = this.props
    // let params= {
    //   id: this.$router.params.topicid,
    //   mdrender: true,
    //   accessToken: user.accessToken
    // }
    // this.props.onGetTopicInfo(params)
  }
  getDetail(){
    let {user} = this.props
    let params= {
      id: this.$router.params.topicid,
      mdrender: true,
      accesstoken: user.accessToken
    }
    this.props.onGetTopicInfo(params)
  }
  componentWillReceiveProps(nextProps){
    if(this.props.playStatus !== nextProps.playStatus){
      this.getDetail()
    }
  }
  render(){
    let {topicInfo, replyTopicInfo, user, onPlayTopicNice} = this.props
    return(
      <View>
        <TopicInfo info={topicInfo} reply={replyTopicInfo} user={user} onPlayTopicNice={onPlayTopicNice} onDetail={this.getDetail.bind(this)} />
      </View>
    )
  }
}

export default Detail